<template>

</template>
<script>
import Breadcrumb from '../component/breadThump.vue'
import * as service from "@/views/distribute/service";
import {moveWindowLayout} from "@/views/scene/service";

export default {
  components: {
    Breadcrumb
  },
  data() {
    return {
      statusProp: false,
      statusIsDelFileProp: false,
      foldIDCache: {},
      isShowEditFile: false,
      editForm: {
        mediaName: '',
        location: '',
        image: '',
        groupId: '',
        cleanProcess: '',
        mediaLink: '',
        auditHistory: []
      },
      dragEnterCache: {
        file_type: ''
      },
      historyRoute: [],
      selectTerList: []
    }
  },
  watch: {
    "$route.query.parentId"(val) {
      if(val == undefined) {
        let arr = [];
        let historyRoute = this.historyRoute[0];
        arr.push(historyRoute)
        this.historyRoute = arr;
        window.localStorage.setItem('historyRoute', JSON.stringify(arr))
        this.$refs.BreadcrumbRef.refreshMenu()
      }
      let path = this.$route.path
      if(path !== '/dashboard') {
        if(val) {
          this.searchObj.parentId = val
          this.getList()
        } else {
          this.searchObj.parentId = 0
          this.getList()
        }
      }
    }
  },
  mounted() {
    let path = this.$route.path
    console.log('path ---', path)

    this.initHistoryRouter()
    if(path !== '/dashboard') {
      if(this.$route.query.parentId) {
        this.searchObj.parentId = this.$route.query.parentId
        this.getList()
      } else {
        this.searchObj.parentId = 0
        this.getList()
      }
    }
  },
  methods: {
    initHistoryRouter() {
      this.historyRoute = []
      let path = this.$route.path
      if(path.indexOf('/distribute/video') !== -1) {
        this.historyRoute.push({
          label: '视频',
          page: '/distribute/video'
        })
      } else if(path.indexOf('/distribute/picture') !== -1) {
        this.historyRoute.push({
          label: '图片',
          page: '/distribute/picture'
        })
      } else if(path.indexOf('/distribute/webApplication') !== -1) {
        this.historyRoute.push({
          label: '网页应用',
          page: '/distribute/webApplication'
        })
      } else if(path.indexOf('/distribute/officeFile') !== -1) {
        this.historyRoute.push({
          label: '文档',
          page: '/distribute/officeFile'
        })
      } else if(path.indexOf('/distribute/applicationProgram') !== -1) {
        this.historyRoute.push({
          label: '应用程序',
          page: '/distribute/applicationProgram'
        })
      } else if(path.indexOf('/distribute/desktopPicture') !== -1) {
        this.historyRoute.push({
          label: '电脑桌面',
          page: '/distribute/desktopPicture'
        })
      } else if(path.indexOf('/distribute/others') !== -1) {
        this.historyRoute.push({
          label: '其他',
          page: '/distribute/others'
        })
      } else if(path.indexOf('/distribute/streaming') !== -1) {
        this.historyRoute.push({
          label: '流媒体',
          page: '/distribute/streaming'
        })
      }
      window.localStorage.setItem('historyRoute', JSON.stringify(this.historyRoute))
      if(this.$refs.BreadcrumbRef) {
        this.$refs.BreadcrumbRef.refreshMenu()
      }
    },
    deleteOtherRouteMethods(msg) {
      const findIndex = this.historyRoute.findIndex((result) => {
        return result.page === msg.page
      })
      if(findIndex !== -1) {
        if(findIndex === 0) {
          this.initHistoryRouter()
        } else {
          this.historyRoute = this.historyRoute.slice(0, findIndex + 1)
        }
      }
      window.localStorage.setItem('historyRoute', JSON.stringify(this.historyRoute))
      this.$refs.BreadcrumbRef.refreshMenu()
    },
    sendFile(row) {
      this.clickCache = Object.assign({}, row)
      this.showTerDialog = true
    },
    // 移动到最外层
    moveToPrevParent(row) {
      const params = {
        id: row.id,
        fileType: row.file_type,
        parentId: 0
      }

      service.saveFileContent(params).then(() => {
        this.$message.success('操作成功')
        this.getList()
      })
    },
    updateFileInFold(foldId, fileObj) {
      console.log('fileObj ---', fileObj)
      const params = {
        id: fileObj.id,
        fileType: fileObj.file_type,
        parentId: foldId
      }

      service.saveFileContent(params).then(() => {
        this.$message.success('操作成功')
        this.getList()
        this.dragEnterCache = {}
        this.foldIDCache = {}
      })
    },
    dropNewHandler(ev, row) {
      console.log('拖拽 ---', this.dragEnterCache, row)
      this.updateFileInFold(row.id, this.dragEnterCache)
    },
    dragstartMethods(row) {
      this.dragEnterCache = {...row}
    },
    editFile(row) {
      console.log('row', row);
      this.isShowEditFile = true
      this.editForm.id = row.id
      this.editForm.groupId = row.groupId || row.group_id
      this.editForm.mediaName = row.noSuffixMediaName
      this.editForm.location = row.location
      this.editForm.image = row.image
      this.editForm.mediaSuffix = row.mediaSuffix || row.media_suffix
      this.editForm.fileType = row.file_type
      this.editForm.cleanProcess = row.clean_process
      this.editForm.mediaLink = row.media_link
      this.editForm.auditHistory = row.auditHistory

    },

    dragEnd(ev, row, ref) {
      ev.preventDefault();
      // 文件夹类型
      if(this.dragEnterCache.file_type == 1) {
        this.updateFileInFold(this.dragEnterCache.id, this.foldIDCache.id)
      }
      this.containerCacheList.forEach((item)=>{
        item.removeEventListener('dragleave',this.onDragleave)
        item.removeEventListener('dragenter', this.onDragenter)
      })
      this.containerCacheList = []
      this.foldIDCache = {}
      this.dragEnterCache = {}
    },

    onDragleave(ev) {
      console.log('leave ---', ev)
    },
    dragStart(ev, row, ref) {
      console.log('开始拖拽 ---')
      this.foldIDCache = Object.assign({}, row)
      const container = document.querySelectorAll('.el-table__row')
      container.forEach((item)=>{
        item.addEventListener('dragleave',this.onDragleave)
        item.addEventListener('dragenter', this.onDragenter)
      })
      this.containerCacheList = container
    },
    showDelWarn(row) {
      this.clickCache = Object.assign({}, row)
      this.statusIsDelFileProp = true
    },
    cancelHandler() {
      this.statusIsDelFileProp = false
      this.clickCache = {}
      this.mutiDelList = []
    },
    closeHandlerMethods() {
      this.statusProp = false
    },
    confirmDelMethods() {
      this.statusProp = true
    },

    confirmDelOnlyHandler() {
      this.delService(false)
    },

    confirmAllDelMethods() {
      this.delService(true)
    },

    delService(terminalDelete) {
      if(this.mutiDelList.length > 0) {
        const params = {
          ids: [],
          terminalDelete
        }
        this.mutiDelList.forEach((item) => {
          params.ids.push(item.id)
        })

        service.delFileContent(params).then(() => {
          this.$message.success('删除成功')
          this.getList()
          this.delWindowDialog = false
          this.statusProp = false
          this.statusIsDelFileProp = false
          this.mutiDelList = []
        })
      } else {
        const params = {
          ids: [this.clickCache.id],
          terminalDelete
        }
        service.delFileContent(params).then(() => {
          this.$message.success('删除成功')
          this.getList()
          this.delWindowDialog = false
          this.statusProp = false
          this.statusIsDelFileProp = false
          this.clickCache = {}
        })
      }
    },

    mutiSendFileMethods() {
      if(this.mutiDelList.length === 0) {
        this.$message.error('请选择分发的文件')
        return
      }
      this.showTerDialog = true
    },
    mutiDelHandler() {
      if(this.mutiDelList.length === 0) {
        this.$message.error('请选择删除的分发内容')
        return
      }
      this.statusProp = true
    },
    cancelTerDialog() {
      this.showTerDialog = false
    },
    confirmSelectTer(msg, mediaIdFields='id') {
      let clientIds = []
      let mediaIds = []
      msg.forEach((item) => {
        // if(clientIds.indexOf(item.id) === -1) {
        //   if(item.status == 0) {
        //     this.$message.error(`终端${item.name}已离线`)
        //   } else {
        //     clientIds.push(item.id)
        //   }
        // }
        clientIds.push(item.id)
      })

      if(this.clickCache && this.clickCache[mediaIdFields]) {
        mediaIds.push(this.clickCache[mediaIdFields])
      } else {
        this.mutiDelList.forEach((item) => {
          if(mediaIds.indexOf(item[mediaIdFields]) === -1) {
            mediaIds.push(item[mediaIdFields])
          }
        })
      }

      const params = {
        clientIds,
        mediaIds
      }
      if(clientIds.length > 0) {
        // this.$refs.terListDialogRef.getFileRateList()
        service.quicklySend(params).then(() => {
          this.$message.success('发送成功')
          // console.log('this.$refs.terListDialogRef ---', this.$refs.terListDialogRef)
          this.$refs.terListDialogRef.getFileRateList()
          // this.showTerDialog = false
        })
      }
    },
    pushRouterHistory(row, type) {
      this.$router.push(`/distribute/${type}?parentId=${row.id}`)
      this.historyRoute.push({
        label: row.noSuffixMediaName,
        page: `/distribute/${type}?parentId=${row.id}`
      })
      window.localStorage.setItem('historyRoute', JSON.stringify(this.historyRoute))
    }
  }
}
</script>
